import {configureStore, createSlice} from '@reduxjs/toolkit'
export const authSlice = createSlice({
  name: 'auth',
  initialState: {
    isLogin: localStorage.getItem('isLogin') || false,
    token: null,
    user: JSON.parse(localStorage.getItem('user')) || null
  },
  reducers: {
    login(state, action) {
      state.isLogin = true
      state.token = action.payload.token
      state.user = action.payload.user
      localStorage.setItem('isLogin', state.isLogin)
      localStorage.setItem('user', JSON.stringify(state.user))
    },
    logout(state, action){
      state.isLogin = false
      state.token = ''
      state.user = null
      localStorage.clear()
    }
  }
})
export const {login, logout} = authSlice.actions

export default configureStore({
  reducer: authSlice.reducer
})